package com.group3.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group3.dao.AttendanceResultMapper;
import com.group3.dao.LessonMapper;
import com.group3.dao.LessonTeacherMapper;
import com.group3.domain.*;
import com.group3.pojo.receive.AttendanceReceive;
import com.group3.pojo.result.AttendanceInfoResult;
import com.group3.utils.NotNull;
import com.group3.utils.Result;
import com.group3.utils.ResultUtil;
import com.group3.service.AAttendanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AAttendanceServiceImpl implements AAttendanceService {

    @Resource
    private AttendanceResultMapper attendanceResultMapper;
    @Resource
    private LessonTeacherMapper lessonTeacherMapper;
    @Resource
    private LessonMapper lessonMapper;

    //查询考勤记录待完成
    @Override
    @Transactional
    public Result selectAttendance(AttendanceReceive attendanceReceive) {

        AttendanceResultExample attendanceResultExample = new AttendanceResultExample();
        AttendanceResultExample.Criteria criteria = attendanceResultExample.createCriteria();

        // TODONE 查询条件需要改进！
        if (NotNull.judge(attendanceReceive.getProfessional())){//传入了专业名
            criteria.andProfessionNameEqualTo(attendanceReceive.getProfessional());
        }
        if (NotNull.judge(attendanceReceive.getGrade())){//传入了年级
            criteria.andGradeEqualTo(attendanceReceive.getGrade());
        }
        if (NotNull.judge(attendanceReceive.getKlass())){//传入了班级
            criteria.andClassIdEqualTo(attendanceReceive.getKlass());
        }
        if (NotNull.judge(attendanceReceive.getStartTime())){//传入了时间段
            criteria.andDateGreaterThanOrEqualTo(attendanceReceive.getStartTime());
        }
        if (NotNull.judge(attendanceReceive.getEndTime())){
            criteria.andDateGreaterThanOrEqualTo(attendanceReceive.getEndTime());
        }
        if (NotNull.judge(attendanceReceive.getDepartments())){//传入了院系
            criteria.andDepartNameEqualTo(attendanceReceive.getDepartments());
        }
        if (NotNull.judge(attendanceReceive.getLessonName())){//传入了课程名
            criteria.andLessonNameEqualTo(attendanceReceive.getLessonName());
        }
        if (NotNull.judge(attendanceReceive.getType())){//传入了考勤状态
            criteria.andAttendanceTypeEqualTo(attendanceReceive.getType());
        }
        if (NotNull.judge(attendanceReceive.getStudentName())){//传入了学生姓名
            criteria.andStudentNameLike("%"+(attendanceReceive.getStudentName())+"%");
        }
        if (NotNull.judge(attendanceReceive.getStudentId())){//传入了学生id
            criteria.andStudentIdEqualTo(attendanceReceive.getStudentId());
        }
        if (NotNull.judge(attendanceReceive.getTeacherZ())){//传入了责任老师
            criteria.andTeacherIdEqualTo(attendanceReceive.getTeacherZ());
        }
        if (NotNull.judge(attendanceReceive.getGroupId())){//传入了项目组
            criteria.andGroupIdEqualTo(attendanceReceive.getGroupId());
        }
//        //根据实训老师添加条件
//        if (NotNull.judge(attendanceReceive.getTeacherS())) {
//            criteria.andTeacherIdEqualTo(attendanceReceive.getTeacherS());
//        }

        //根据实训老师查询课程（lesson_teacher表）
        LessonTeacherExample lessonTeacherExample = new LessonTeacherExample();
        lessonTeacherExample.setDistinct(true);

        // 老师查询考勤时，该teacherId为查询老师的id（andTeacherIdEqualTo）
        lessonTeacherExample.createCriteria()
                .andTeacherIdLike("%"+(attendanceReceive.getTeacherS())+"%");

        //得到一个课程集合
        List<LessonTeacherKey> lessonTeacherKeys
                = lessonTeacherMapper.selectByExample(lessonTeacherExample);

        //没有数据时，返回空
        if (lessonTeacherKeys.isEmpty()){
            return ResultUtil.success("查询成功", 0 );
        }

//        取出课程名集合
        List<String> lessonName = new ArrayList<>();
        for (LessonTeacherKey lessonTeacherKey:lessonTeacherKeys) {
            lessonName.add(lessonTeacherKey.getLessonName());
        }

        //根据课程集合和其他条件查询考勤视图实现

        criteria.andLessonNameIn(lessonName);
        attendanceResultExample.setOrderByClause("date");
//        1.先开启分页
        PageHelper.startPage(attendanceReceive.getPageNumber(), attendanceReceive.getPageSize());//启动分页
//        2.执行SQL语句
        List<AttendanceInfoResult> attendanceResults = attendanceResultMapper.selectAttendanceInfo(attendanceResultExample);
//        3.获取分页信息
        PageInfo<AttendanceInfoResult> klassResultsPageInfo = new PageInfo<>(attendanceResults);//获取分页信息


        return ResultUtil.success("查询成功", attendanceResults, klassResultsPageInfo.getTotal());
    }
}
